TOPNAME = ysyx_22040213_top
INC_PATH =$(abspath ./include)

VERILATOR = verilator
VERILATOR_CFLAGS += -Wall -MMD --build -cc -trace-fst \
				-O3 --x-assign fast --x-initial fast --noassert

BUILD_DIR = ./build
OBJ_DIR = $(BUILD_DIR)/obj_dir
BIN = $(BUILD_DIR)/$(TOPNAME)

default: $(BIN)

$(shell mkdir -p $(BUILD_DIR))


# project source
VSRCS = $(shell find $(abspath ./vsrc) -name "*.v")
CSRCS = $(shell find $(abspath ./csrc) -name "*.c" -or -name "*.cpp" -or -name "*.cc")

# rules for verilator
INCFLAGS = $(addprefix -I, $(INC_PATH))
CFLAGS += $(INCFLAGS) -DTOP_NAME="\"V$(TOPNAME)\"" $(shell llvm-config --cxxflags) -fPIE 

LDFLAGS += -lSDL2 -lSDL2_image -lreadline $(shell llvm-config --libs) -ldl

# rules for others
include $(NPC_HOME)/scripts/npc.mk
include $(NPC_HOME)/scripts/difftest.mk

#rules for execute sim_main
DIFF_REF_PATH = $(NEMU_HOME)
DIFF_REF_SO = $(DIFF_REF_PATH)/build/riscv64-nemu-interpreter-so
ARGS_DIFF = --diff=$(DIFF_REF_SO)
IMG ?=
ARGS ?=
SIM_MAIN_EXEC = --log=$(BUILD_DIR)/nemu-log.txt $(ARGS_DIFF)  
SIM_MAIN_EXEC += $(ARGS) $(IMG) 


$(BIN): $(VSRCS) $(CSRCS)
	@rm -rf $(OBJ_DIR)
	$(VERILATOR) $(VERILATOR_CFLAGS) \
		-top $(TOPNAME) $^ \
		$(addprefix -CFLAGS , $(CFLAGS))  $(addprefix -LDFLAGS , $(LDFLAGS)) \
	--Mdir $(OBJ_DIR) --exe -o $(abspath $(BIN))


run: $(BIN)
	@$^ $(SIM_MAIN_EXEC)
	$(call git_commit, "sim RTL") # DO NOT REMOVE THIS LINE!!!



clean:
	rm -rf $(BUILD_DIR)

.PHONY: clean run
